package com.auto.learning.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.MyMoneyActivity;
import com.auto.learning.activity.NewBookListActivity;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.activity.TopBookListActivity;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.DownLoadView;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.bean.ShareBean;
import com.auto.learning.event.BuySuccessEvent;
import com.auto.learning.event.CollectEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.event.ProgressEvent;
import com.auto.learning.event.listener.DialogListener;
import com.auto.learning.net.HttpCommonInfo;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.DownLoadManager;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.CommonAlertDialog;
import com.auto.learning.view.SharedPreferencesUtils;
import com.baidu.mobstat.StatService;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;
import com.auto.learning.pay.UserBuyUtils;;

public class HomePageListViewAdapter extends BaseAdapter {
	
	private ArrayList<BookBean> list;
	private ArrayList<BookBean> newList;
	private ArrayList<BookBean> topList;
	private Context context;
	//0 代表在homePage和chooseBook页面，1代表在收藏页面下的收藏fragment下,2为我的下载页面,3为主播页面列表,4为搜索出的书的列表,5为首页列表，默认为0
	private int localType = 0;  
	private boolean isShowCheckBox = false;
	private ArrayList<Integer> selectedBookIds = new ArrayList<Integer>();
	DisplayImageOptions options;
	ProgressEvent progress;
	
	@SuppressLint("UseSparseArrays")
	private Map<Integer, DownLoadView> map = new HashMap<Integer, DownLoadView>();

	private Handle handle;
	
	@SuppressLint("HandlerLeak")
	private Handle handler = new Handle(context){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 100:
				ProgressEvent event = (ProgressEvent) msg.obj;
				DownLoadView downLoadView = map.get(progress.getBookId());
				if (downLoadView != null ) {
					if (downLoadView.getTextView().getTag().toString().equals(""+progress.getBookId())) {
						downLoadView.getTextView().setText(event.getCurrentRatio()+"%");
						downLoadView.getImageView().setBackgroundResource(R.anim.download_stute);
						AnimationDrawable _animaition = (AnimationDrawable)downLoadView.getImageView().getBackground();
						_animaition.setOneShot(false);   
						_animaition.start();
					}
				}
				break;
			case HttpRequestHelper.COLLECT_SUCCESS:
				ResponeResult collect_result = (ResponeResult)msg.obj;
				CollectEvent collect_event = new CollectEvent();
				collect_event.setBookId(Integer.parseInt(collect_result.getData()));
				collect_event.setCollect(true);
				EventBus.getEventBus(HttpRequestHelper.COLLECT_EVENT).post(collect_event);
				break;
			case HttpRequestHelper.COLLECT_FAIL:
				if (msg.obj != null) {
					CommonMethod.makeNotice(context, msg.obj+"");
				}
				break;
			case 1:
				int m_bookId = msg.arg1;
				if (list != null) {
					int size = list.size();
					for (int i = 0; i < size; i++) {
						if (m_bookId == 0) {
							list.get(i).setNeedBuy(1);
						} else if (list.get(i).getBookId() == m_bookId) {
							list.get(i).setNeedBuy(1);
						}
					}
					notifyDataSetChanged();
				}
				break;
			
			}
		};
	};

	
	public boolean getIsShowCheckBox() {
		return isShowCheckBox;
	}

	public void setIsShowCheckBox(boolean isShowCheckBox) {
		this.isShowCheckBox = isShowCheckBox;
	}

	public HomePageListViewAdapter(Context context,ArrayList<BookBean> list, ArrayList<BookBean> newList, ArrayList<BookBean> topList){
		this.context = context;
		this.list = list;
		this.newList = newList;
		this.topList = topList;
		registerEvent();
		initImageLoader();
	}
	
	public HomePageListViewAdapter(Context context,ArrayList<BookBean> list){
		this.context = context;
		this.list = list;
		registerEvent();
		initImageLoader();
	}
	
	public HomePageListViewAdapter(Context context,ArrayList<BookBean> list,int localType){
		this.context = context;
		this.list = list;
		this.localType = localType;
		registerEvent();
		initImageLoader();
	}
	
	public HomePageListViewAdapter(Context context,ArrayList<BookBean> list,int localType,Handle handle){
		this.context = context;
		this.list = list;
		this.localType = localType;
		this.handle = handle;
		registerEvent();
		initImageLoader();
	}
	
	public HomePageListViewAdapter(Context context,ArrayList<BookBean> list,int localType, boolean isShowCheckBox){
		this.context = context;
		this.list = list;
		this.localType = localType;
		this.isShowCheckBox = isShowCheckBox;
		registerEvent();
		initImageLoader();
	}
	
	@Subscribe
	public void onEventProgress(ProgressEvent event){
		progress = event;
		handler.obtainMessage(100, event).sendToTarget();
	}
	
	@Subscribe
	public void onEventRefresh(Integer t){
		notifyDataSetChanged();
	}
	
	@Subscribe
	public void onEventBuySuccess(BuySuccessEvent buySuccessEvent){
		handler.obtainMessage(1, buySuccessEvent.getBookId(), 0).sendToTarget();
	}
	
	public void registerEvent(){
		EventBus.getEventBus("downloadProgress",ThreadEnforcer.MAIN).register(this);
		EventBus.getEventBus("downloadRefresh",ThreadEnforcer.MAIN).register(this);
		EventBus.getEventBus("m_buy_success",ThreadEnforcer.MAIN).register(this);
	}
	

	@Override
	public int getCount() {
		int tempCount = 0;
		if (newList != null && newList.size() > 0) {
			tempCount ++;
		}
		if (topList != null && topList.size() > 0) {
			tempCount ++;
		}
		return list.size() + tempCount;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		if (list.size() > position) {
			return list.get(position).getBookId();
		}
		return 0;
	}

	ViewHolder holder;
	ViewBottomHolder viewBottomHolder;
	ViewTipHolder viewTipHolder;
	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (list.size() > position) {
			final BookBean bean = list.get(position);
			if (bean.getBookId() > 0) {
				if (!(convertView != null && convertView.getTag() instanceof ViewHolder)) {
					if (localType == 4) {
						convertView = LayoutInflater.from(context).inflate(R.layout.first_page_adapter_search, null);
					}else {
						convertView = LayoutInflater.from(context).inflate(R.layout.first_page_adapter, null);
					}
					holder = new ViewHolder();
					holder.f_image_face = (ImageView) convertView.findViewById(R.id.f_image_face);
					holder.f_image_play = (ImageView) convertView.findViewById(R.id.f_image_play);
					holder.f_image_guoqi = (ImageView) convertView.findViewById(R.id.f_image_guoqi);
					holder.tv_bookname = (TextView) convertView.findViewById(R.id.tv_bookname);
					holder.tv_slogan = (TextView) convertView.findViewById(R.id.tv_slogan);
					holder.tv_listen_number = (TextView) convertView.findViewById(R.id.tv_listen_number);
					holder.tv_shoucang_number = (TextView) convertView.findViewById(R.id.tv_shoucang_number);
					holder.tv_pinglun_number = (TextView) convertView.findViewById(R.id.tv_pinglun_number);
					holder.linear_share = (LinearLayout) convertView.findViewById(R.id.linear_share);
					holder.linear_collect = (LinearLayout) convertView.findViewById(R.id.linear_collect);
					holder.linear_download = (LinearLayout) convertView.findViewById(R.id.linear_download);
					holder.linear_read = (LinearLayout) convertView.findViewById(R.id.linear_read);
					holder.linear_comment = (LinearLayout) convertView.findViewById(R.id.linear_comment);
					holder.cb_choose = (CheckBox) convertView.findViewById(R.id.cb_choose);
					holder.linear_right = (LinearLayout) convertView.findViewById(R.id.linear_right);
					holder.tv_download =  (TextView) convertView.findViewById(R.id.tv_download);
					holder.image_download = (ImageView) convertView.findViewById(R.id.image_download);
					holder.tv_collect =  (TextView) convertView.findViewById(R.id.tv_collect);
					holder.image_collect = (ImageView) convertView.findViewById(R.id.image_collect);
					holder.linear_operator_top = (LinearLayout) convertView.findViewById(R.id.linear_operator_top);
					holder.image_is_read = (ImageView) convertView.findViewById(R.id.image_is_read);
					holder.f_image_is_new = (ImageView) convertView.findViewById(R.id.f_image_is_new);
					holder.image_price = (ImageView) convertView.findViewById(R.id.image_price);
					holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
					holder.linear_price = (LinearLayout) convertView.findViewById(R.id.linear_price);
					holder.tv_need_price = (TextView) convertView.findViewById(R.id.tv_need_price);
					convertView.setTag(holder);
				}else {
					holder = (ViewHolder) convertView.getTag();
				}
				if (localType != 4) {
				  switch (localType) {
				  	case 5:
					case 3:
					case 0:
						holder.linear_share.setVisibility(View.GONE);
//						holder.linear_collect.setVisibility(View.VISIBLE);
						holder.linear_comment.setVisibility(View.VISIBLE);
						break;
					case 1:
						holder.linear_share.setVisibility(View.VISIBLE);
//						holder.linear_collect.setVisibility(View.GONE);
						holder.linear_comment.setVisibility(View.GONE);
						break;
					case 2:
						holder.linear_comment.setVisibility(View.VISIBLE);
//						holder.linear_collect.setVisibility(View.VISIBLE);
						holder.linear_download.setVisibility(View.GONE);
						holder.linear_share.setVisibility(View.GONE);
						break;
					}
				  	holder.linear_collect.setVisibility(View.GONE);
					/*if (bean.getIsFavor() == 2) {
						holder.tv_collect.setText("已收藏");
						holder.image_collect.setImageResource(R.drawable.shoucang_ok);
					}else {
						holder.tv_collect.setText("收藏");
						holder.image_collect.setImageResource(R.drawable.shoucang);
					}*/
					
					if (TemporaryVariable.downLoadedList.contains(bean.getBookId())) {
						holder.tv_download.setText("已下载");
						holder.image_download.setBackgroundResource(R.drawable.xiazaiok);
						holder.linear_price.setVisibility(View.GONE);
						holder.linear_download.setVisibility(View.VISIBLE);
					}else {
						if (bean.getNeedBuy() == 2) {
							holder.linear_price.setVisibility(View.VISIBLE);
							holder.linear_download.setVisibility(View.GONE);
						} else {
							holder.linear_price.setVisibility(View.GONE);
							holder.linear_download.setVisibility(View.VISIBLE);
							if (TemporaryVariable.temp_download_all_list.contains(bean.getBookId())) {
								holder.tv_download.setText("0%");
								holder.image_download.setBackgroundResource(R.anim.download_stute);
							}else {
								holder.tv_download.setText("下载");
								holder.image_download.setBackgroundResource(R.drawable.xiazai);
							}
						}
					}
					
					if (bean.getFree() == 2) {
						holder.image_price.setVisibility(View.GONE);
						holder.tv_need_price.setVisibility(View.GONE);
					}else {
						holder.tv_need_price.setText(bean.getPriceTag()+"");
						holder.image_price.setVisibility(View.VISIBLE);
						holder.tv_need_price.setVisibility(View.VISIBLE);
					}
				}
				
				holder.linear_right.setTag(bean.getBookId());
				holder.cb_choose.setTag(bean.getBookId());
				if (isShowCheckBox) {
					holder.cb_choose.setVisibility(View.VISIBLE);
					holder.linear_right.setEnabled(true);
				}else {
					holder.cb_choose.setVisibility(View.GONE);
					holder.linear_right.setEnabled(false);
				}
				if (selectedBookIds.size() > 0) {
					if (selectedBookIds.contains(bean.getBookId())) 
						holder.cb_choose.setChecked(true);
					else
						holder.cb_choose.setChecked(false);
				} else {
					holder.cb_choose.setChecked(false);
				}
				if (bean.getIsRead() == 2) {
					holder.image_is_read.setVisibility(View.VISIBLE);
					holder.image_is_read.setImageResource(R.drawable.yidu);
				}else {
					holder.image_is_read.setVisibility(View.GONE);
				}
				holder.tv_bookname.setText(bean.getBookName()+"");
				holder.tv_slogan.setText(bean.getSlogan());
				holder.tv_shoucang_number.setText(bean.getFavorNum()+"");
				holder.tv_listen_number.setText(bean.getListenNum()+"");
				holder.tv_pinglun_number.setText(bean.getCommentNum()+"");
				if (localType == 4) {
					ImageLoader.getInstance().displayImage(bean.getFaceImgPlay(),holder.f_image_face,options);
				}else {
					ImageLoader.getInstance().displayImage(bean.getFaceImg(),holder.f_image_face,options);
				}
				ImageLoader.getInstance().displayImage(bean.getNationImg(),holder.f_image_guoqi);
				int playingBookId[] = MusicPlayManager.getInstance(context).isPlayingBookId();
				if (playingBookId[0] != 0 && playingBookId[0] == bean.getBookId()) {
					if (playingBookId[2] == 0){
						holder.f_image_play.setImageResource(R.drawable.playing);
					}else{
						holder.f_image_play.setImageResource(R.drawable.play);
					}
					holder.linear_right.setBackgroundColor(context.getResources().getColor(R.color.main_green_color));
					holder.linear_right.getBackground().setAlpha(40);
				}else {
					holder.f_image_play.setImageResource(R.drawable.play);
					holder.linear_right.setBackgroundColor(context.getResources().getColor(R.color.main_backgroud_item));
				}
				holder.f_image_play.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						final MusicPlayManager manager = MusicPlayManager.getInstance(context);
						int ids[] = manager.isPlayingBookId();
						if (ids[0] == 0 || ids[0] != bean.getBookId()) {
							if( bean.getNeedBuy() == 2 && !TemporaryVariable.downLoadedList.contains(bean.getBookId())){
								new UserBuyUtils(((Activity)context), bean.getBookId(), bean.getPriceTag(),bean.getBookName()).buy();
								return;
							}
							playDialog(bean);
						}else {
							manager.pause();
						}
					}
				});
				if (isShowCheckBox) {
					holder.linear_operator_top.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							onItemClick(bean.getBookId());
						}
					});
				}
				
				/*holder.linear_operator_top.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if (isShowCheckBox) {
							onItemClick(bean.getBookId());
						}else {
							final MusicPlayManager manager = MusicPlayManager.getInstance(context);
							int ids[] = manager.isPlayingBookId();
							if (ids[0] == 0 || ids[0] != bean.getBookId()) {
								playDialog(bean);
							}else {
								manager.pause();
							}
							
							
							TemporaryVariable.read_list.clear();
							for (int i = 0; i < list.size(); i++) {
								TemporaryVariable.read_list.add(list.get(i).getBookId());
							}
							if (localType == 3 && TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
								TemporaryVariable.playActivity.finish();
							}
							Intent intent = new Intent(context, PlayActivity.class);
							intent.putExtra("bookId", bean.getBookId());
							intent.putExtra("faceImg", bean.getFaceImg());
							intent.putExtra("type", 1);
							context.startActivity(intent);
						}
					}
				});*/
				
				holder.linear_operator_top.setOnLongClickListener(new OnLongClickListener() {
					
					@Override
					public boolean onLongClick(View v) {
						return false;
					}
				});
				holder.linear_collect.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						HttpRequestHelper.AddCollect(context, handler, bean.getBookId(),HttpRequestHelper.COLLECT_FAIL);
					}
				});
				
				holder.linear_comment.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						/*Intent intent = new Intent(context, CommentActivity.class);
						intent.putExtra("bookId", bean.getBookId());
						intent.putExtra("bookName", bean.getBookName()+"");
						intent.putExtra("imgFace", bean.getFaceImg()+"");
						intent.putExtra("imgFacePlay", bean.getFaceImgPlay()+"");
						context.startActivity(intent);*/
						StatService.onEvent(context, "LogEventTypeCommentButtonClick", "评论按钮");

						TemporaryVariable.read_list.clear();
						for (int i = position; i < list.size(); i++) {
							if (list.get(i).getBookId() > 0 && list.get(i).getNeedBuy() != 2) {
								TemporaryVariable.read_list.add(list.get(i).getBookId());
							}else {
								
								break;
							}
						}
						if (localType == 3 && TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
							TemporaryVariable.playActivity.finish();
						}
						Intent intent = new Intent(context, PlayActivity.class);
						intent.putExtra("bookId", bean.getBookId());
						intent.putExtra("faceImg", bean.getFaceImg());
						intent.putExtra("type", 2);
						intent.putExtra("canRead",bean.getCanRead());
						intent.putExtra("needBuy", bean.getNeedBuy());
						context.startActivity(intent);
					}
				});
				
				holder.linear_price.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						new UserBuyUtils(context, bean.getBookId(), bean.getPriceTag() , UserBuyUtils.OPERATOR_BUY,bean.getBookName()).buy();
					}
				});
				
				holder.linear_download.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if (!TemporaryVariable.downLoadedList.contains(bean.getBookId()) && CommonMethod.checkUserLogin((Activity)context)) {
							StatService.onEvent(context, "LogEventTypeDownloadButtonClic", "下载按钮");
							EventBus.getEventBus("pause_download").post(false);
							int size = bean.getProSize() + bean.getDoubiSize() + bean.getEnSize();
							if (size > 0) {
		//						DownLoadManager.getInstance(context).setNeedDownLoadBookId(bean.getBookId()).prepareBefore(size);
								DownLoadManager.getInstance(context).setContext(context).prepareBefore(bean.getBookId(),size);
							}
						}
					}
				});
				holder.linear_read.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						StatService.onEvent(context, "LogEventTypeDetailButtonClick", "详情按钮");
						TemporaryVariable.read_list.clear();
						for (int i = position; i < list.size(); i++) {
							if (list.get(i).getBookId() > 0) {
								if (TemporaryVariable.downLoadedList.contains(list.get(i).getBookId()) || list.get(i).getNeedBuy() != 2) {
									TemporaryVariable.read_list.add(list.get(i).getBookId());
								}
							}else {
								break;
							}
						}
						if (localType == 3 && TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
							TemporaryVariable.playActivity.finish();
						}
						Intent intent = new Intent(context, PlayActivity.class);
						intent.putExtra("bookId", bean.getBookId());
						intent.putExtra("canRead",bean.getCanRead());
						intent.putExtra("needBuy", bean.getNeedBuy());
						intent.putExtra("faceImg", bean.getFaceImg());
						intent.putExtra("type", 1);
						context.startActivity(intent);
					}
				});
				holder.cb_choose.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						int tempId = Integer.parseInt(v.getTag().toString());
						if (selectedBookIds.contains(tempId)) {
							selectedBookIds.remove(selectedBookIds.indexOf(tempId));
						}else {
							selectedBookIds.add(tempId);
						}
						EventBus.getEventBus("delete_num").post(selectedBookIds.size());
					}
				});
				holder.linear_share.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ShareBean shareBean = new ShareBean();
						shareBean.setBookId(bean.getBookId());
						shareBean.setUrl(HttpCommonInfo.MURL+"authorware-detail?id=" + bean.getBookId());
						shareBean.setContent(bean.getSlogan());
						shareBean.setTitle(bean.getBookName());
						shareBean.setImageUrl(bean.getFaceImgPlay());
						EventBus.getEventBus("m_share").post(shareBean);
					}
				});
				holder.tv_download.setTag(bean.getBookId());
				holder.image_download.setTag(bean.getBookId());
				if (bean.getIsNew() == 2) {
					holder.f_image_is_new.setVisibility(View.VISIBLE);
				}else {
					holder.f_image_is_new.setVisibility(View.GONE);
				}
				map.put(bean.getBookId(), new DownLoadView(bean.getBookId(),holder.tv_download,holder.image_download));
			}else {
				if (!(convertView != null && convertView.getTag() instanceof ViewTipHolder)) {
					convertView = LayoutInflater.from(context).inflate(R.layout.homepage_tip_message, null);
					viewTipHolder = new ViewTipHolder();
					viewTipHolder.tv_groupname = (TextView) convertView.findViewById(R.id.tv_groupname);
					viewTipHolder.rel_top = (RelativeLayout) convertView.findViewById(R.id.rel_top);
					viewTipHolder.tv_week_top = (TextView) convertView.findViewById(R.id.tv_week_top);
					viewTipHolder.tv_month_top = (TextView) convertView.findViewById(R.id.tv_month_top);
					//convertView.setTag(viewTipHolder);
				}else {
					viewTipHolder = (ViewTipHolder) convertView.getTag();
				}
				if (bean.getBookId() == 0) {
					viewTipHolder.tv_groupname.setText(""+bean.getBookName());
					viewTipHolder.rel_top.setVisibility(View.GONE);
					viewTipHolder.tv_groupname.setVisibility(View.VISIBLE);
				}else {
					viewTipHolder.rel_top.setVisibility(View.VISIBLE);
					viewTipHolder.tv_groupname.setVisibility(View.GONE);
					viewTipHolder.tv_week_top.setText("周榜top10");
					viewTipHolder.tv_month_top.setText("月榜top10");
					if (bean.getBookId() == -2) {
						viewTipHolder.tv_week_top.setTextColor(context.getResources().getColor(R.color.main_grake_color));
						viewTipHolder.tv_month_top.setTextColor(context.getResources().getColor(R.color.white));
					}else {
						viewTipHolder.tv_week_top.setTextColor(context.getResources().getColor(R.color.white));
						viewTipHolder.tv_month_top.setTextColor(context.getResources().getColor(R.color.main_grake_color));
					}
					viewTipHolder.tv_week_top.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							handle.sendEmptyMessage(8);
						}
					});
					viewTipHolder.tv_month_top.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							handle.sendEmptyMessage(9);
						}
					});
				}
			}
		 } else if(newList != null && newList.size() > 0 && position == list.size()){
			if (!(convertView != null && convertView.getTag() instanceof ViewBottomHolder)) {
				convertView = LayoutInflater.from(context).inflate(R.layout.fg_first_bottom_adapter, null);
				viewBottomHolder = new ViewBottomHolder();
				viewBottomHolder.gv_book = (GridView) convertView.findViewById(R.id.gv_book);
				viewBottomHolder.tv_grounname = (TextView) convertView.findViewById(R.id.tv_grounname);
				viewBottomHolder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
				viewBottomHolder.tv_mouth_rank = (TextView) convertView.findViewById(R.id.tv_mouth_rank);
				viewBottomHolder.tv_week_rank = (TextView) convertView.findViewById(R.id.tv_week_rank);
				viewBottomHolder.view = convertView.findViewById(R.id.view);
				convertView.setTag(viewBottomHolder);
			}else {
				viewBottomHolder = (ViewBottomHolder) convertView.getTag();
			}
			viewBottomHolder.tv_grounname.setText("最新");
			viewBottomHolder.tv_more.setVisibility(View.VISIBLE);
			viewBottomHolder.tv_mouth_rank.setVisibility(View.GONE);
			viewBottomHolder.view.setVisibility(View.GONE);
			viewBottomHolder.tv_week_rank.setVisibility(View.GONE);
			viewBottomHolder.gv_book.setSelector(new ColorDrawable(Color.TRANSPARENT));
			HomePageNewListAdapter homePageNewListAdapter = new HomePageNewListAdapter(context, newList, 1);
			viewBottomHolder.gv_book.setAdapter(homePageNewListAdapter);
			CommonMethod.setGridViewHeight(viewBottomHolder.gv_book, 3,  CommonMethod.dip2px(context, 8));
			viewBottomHolder.tv_more.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, NewBookListActivity.class);
					context.startActivity(intent);
				}
			});
		} else if(topList != null && topList.size() > 0){
			if (!(convertView != null && convertView.getTag() instanceof ViewBottomHolder)) {
				convertView = LayoutInflater.from(context).inflate(R.layout.fg_first_bottom_adapter, null);
				viewBottomHolder = new ViewBottomHolder();
				viewBottomHolder.gv_book = (GridView) convertView.findViewById(R.id.gv_book);
				viewBottomHolder.tv_grounname = (TextView) convertView.findViewById(R.id.tv_grounname);
				viewBottomHolder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
				viewBottomHolder.tv_mouth_rank = (TextView) convertView.findViewById(R.id.tv_mouth_rank);
				viewBottomHolder.tv_week_rank = (TextView) convertView.findViewById(R.id.tv_week_rank);
				viewBottomHolder.view = convertView.findViewById(R.id.view);
				convertView.setTag(viewBottomHolder);
			}else {
				viewBottomHolder = (ViewBottomHolder) convertView.getTag();
			}
			viewBottomHolder.tv_grounname.setText("热榜");
			viewBottomHolder.tv_more.setVisibility(View.GONE);
			viewBottomHolder.tv_mouth_rank.setVisibility(View.VISIBLE);
			viewBottomHolder.view.setVisibility(View.VISIBLE);
			viewBottomHolder.tv_week_rank.setVisibility(View.VISIBLE);
			viewBottomHolder.gv_book.setSelector(new ColorDrawable(Color.TRANSPARENT));
			HomePageNewListAdapter homePageNewListAdapter = new HomePageNewListAdapter(context, topList, 2);
			viewBottomHolder.gv_book.setAdapter(homePageNewListAdapter);
			CommonMethod.setGridViewHeight(viewBottomHolder.gv_book, 3,  CommonMethod.dip2px(context, 8));
			viewBottomHolder.tv_week_rank.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, TopBookListActivity.class);
					intent.putExtra("type", "1");
					context.startActivity(intent);
				}
			});
			viewBottomHolder.tv_mouth_rank.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, TopBookListActivity.class);
					intent.putExtra("type", "2");
					context.startActivity(intent);
				}
			});
		}
		return convertView;
	}
	
	public void setbookList(ArrayList<BookBean> list){
		this.list = list;
	}
	
	public void itemClick(){
		if (isShowCheckBox) {
			holder.cb_choose.performClick();
		}
	}
	
	/**
	 * 全部勾选
	 */
	public void selectAllItem(){
		for (BookBean item : list) {
			if (!selectedBookIds.contains(item.getBookId())) {
				selectedBookIds.add(item.getBookId());
			}
		}
	}
	
	/**
	 * 清除勾选
	 */
	public void clearSelected(){
		selectedBookIds.clear();
	}
	
	/**
	 * 添加勾选
	 * @param bookId
	 */
	public void addSelected(int bookId){
		if (!selectedBookIds.contains(bookId)) {
			selectedBookIds.add(bookId);
		}
	}
	
	/**
	 * 去除勾选
	 * @param bookId
	 */
	public void removeSelected(int bookId){
		if (selectedBookIds.contains(bookId)) {
			selectedBookIds.remove(selectedBookIds.indexOf(bookId));
		}
	}
	
	public ArrayList<Integer> getSelectedBookIds() {
		return selectedBookIds;
	}

	public void setSelectedBookIds(ArrayList<Integer> selectedBookIds) {
		this.selectedBookIds = selectedBookIds;
	}
	
	/**
	 * item点击事件
	 * @param bookId
	 */
	public void onItemClick(int bookId){
		if (isShowCheckBox) {
			if (selectedBookIds.contains(bookId)) {
				selectedBookIds.remove(selectedBookIds.indexOf(bookId));
			}else {
				selectedBookIds.add(bookId);
			}
			EventBus.getEventBus("delete_num").post(selectedBookIds.size());
		}
	}
	
	
	class ViewHolder{
		public ImageView f_image_face,f_image_guoqi,f_image_play,image_download,image_collect,image_is_read,f_image_is_new,image_price;
		public TextView tv_bookname,tv_slogan,tv_listen_number,tv_shoucang_number,tv_pinglun_number,tv_download,tv_collect,tv_need_price,tv_price;
		public LinearLayout linear_share,linear_collect,linear_download,linear_read,linear_comment,linear_right,linear_operator_top,linear_price;
		public CheckBox cb_choose;
	}
	
	class ViewBottomHolder {
		public TextView tv_grounname,tv_more,tv_mouth_rank,tv_week_rank;
		public GridView gv_book;
		public View view;
	}
	
	class ViewTipHolder {
		public TextView tv_groupname,tv_week_top,tv_month_top;
		private RelativeLayout rel_top;
	}
	
	public Map<Integer, DownLoadView> getMap() {
		return map;
	}

	public void setMap(Map<Integer, DownLoadView> map) {
		this.map = map;
	}
	
	public void showDialog(final BookBean bookBean){
		CommonAlertDialog alertDialog = new CommonAlertDialog(context);
		alertDialog.setTextMessage(String.format(context.getResources().getString(R.string.play_size_notice), CommonMethod.calculationNum(bookBean.getProSize()+bookBean.getEnSize() + bookBean.getDoubiSize())));
		alertDialog.setShowTitle(true);
		String[] a = {context.getResources().getString(R.string.app_continue),context.getResources().getString(R.string.app_cancel)};
		alertDialog.setBtns(new DialogListener() {
			
			@Override
			public void okButtonClick(Dialog dialog) {
				play(bookBean);
				MusicPlayManager.getInstance(context).setShowNetTips(false);
				dialog.dismiss();
			}

			@Override
			public void cancelButtonClick(Dialog dialog) {
				CommonMethod.reordPlayStatus(context, bookBean.getBookId(), true);
				dialog.dismiss();
			}

			@Override
			public void otherButtonClick(Dialog dialog) {
				CommonMethod.reordPlayStatus(context, bookBean.getBookId(), true);
				dialog.dismiss();
			}
		},a);
		alertDialog.show();
	}

	
	public void playDialog(final BookBean bookBean){
		if (TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) {
			play(bookBean);
		}else {
			if ((boolean)SharedPreferencesUtils.getParam(context, HttpRequestHelper.DWONLOAD_PLAY_CONTINUE, false)) {
				play(bookBean);
			} else {
				switch (CommonMethod.getNetworkState(context)) {
				case 0:
					CommonMethod.makeNotice(context, context.getResources().getString(R.string.connect_err_io));
					break;
				case 1:
					play(bookBean);
					break;
				case 2:
					showDialog(bookBean);
					break;
				}
			}
		}
	}
	
	
	private void play(final BookBean bookBean){
	
		StatService.onEvent(context, "LogEventTypeCoverButtonClick", "封面图点击播放事件");
		handler.postDelayed(new Runnable() {
			public void run() {
				ArrayList<BookBean> m_list = new ArrayList<BookBean>();
				int index = list.indexOf(bookBean);
				m_list.add(bookBean);
				for (int k = index - 1; k >= 0; k-- ) {
					if (list.get(k).getBookId() > 0) {
						if (list.get(k).getIsRead() != 2) {
							if (TemporaryVariable.downLoadedList.contains(list.get(k).getBookId()) || (list.get(k).getNeedBuy() != 2)) {
								m_list.add(0,list.get(k));
							}
						}
					}else {
						break;
					}
				}
				for (int i = index + 1; i < list.size(); i++) {
					if (list.get(i).getBookId() > 0) {
						if (list.get(i).getIsRead() != 2) {
							if (TemporaryVariable.downLoadedList.contains(list.get(i).getBookId()) || (list.get(i).getNeedBuy() != 2)) {
								m_list.add(list.get(i));
							}
						}
					}else {
						break;
					}
				}
				
//				if (CommonMethod.getNetworkState(context) == 1 || localType == 2) {
					/*if (localType == 5 || localType == 0) {
						
					} else {
						m_list.addAll(list);
					}*/
				/*}else {
					m_list.add(bookBean);
					int index = list.indexOf(bookBean);
					for (int k = index - 1; k >= 0; k-- ) {
						if (list.get(k).getBookId() > 0 && TemporaryVariable.downLoadedList.contains(list.get(k).getBookId())) {
							m_list.add(0,list.get(k));
						} else {
							break;
						}
					}
					for (int i = index + 1; i < list.size(); i++) {
						if (list.get(i).getBookId() > 0 && TemporaryVariable.downLoadedList.contains(list.get(i).getBookId())) {
							m_list.add(list.get(i));
						} else {
							break;
						}
					}
				}*/
				MusicPlayManager.getInstance(context).setPlayList(m_list).setBookId(bookBean.getBookId()).play();
		
			}
		}, 1500);
		TemporaryVariable.read_list.clear();
		for (int i = list.indexOf(bookBean); i < list.size(); i++) {
			if (list.get(i).getBookId() > 0) {
				if (TemporaryVariable.downLoadedList.contains(list.get(i).getBookId()) || list.get(i).getNeedBuy() != 2) {
					TemporaryVariable.read_list.add(list.get(i).getBookId());
				}
			}else {
				break;
			}
		}
		if (localType == 3 && TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
			TemporaryVariable.playActivity.finish();
		}
		
		Intent intent = new Intent(context, PlayActivity.class);
		intent.putExtra("bookId", bookBean.getBookId());
		intent.putExtra("faceImg", bookBean.getFaceImg());
		intent.putExtra("canRead",bookBean.getCanRead());
		intent.putExtra("needBuy", bookBean.getNeedBuy());
		context.startActivity(intent);
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.book)
		.showImageForEmptyUri(R.drawable.book)
		.showImageOnFail(R.drawable.book).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

}

